package com.itheima.mapper;

import com.itheima.pojo.Order;
import com.itheima.pojo.User;
import com.itheima.pojo.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

// 这个接口中的方法都是用来操作user表
public interface UserMapper {

    @Select("select * from tb_user")
    List<User> findAll();

    @Delete("delete from tb_user where id = #{id}")
    int deleteUserById(int id);

    @Update("update tb_user set username = #{username},birthday=#{birthday},sex=#{sex},address=#{address} where id = #{id}")
    int update(User user);

    @Insert("insert into tb_user values(null,#{username},#{birthday},#{sex},#{address})")
    int save(User user);

    @Select("SELECT * FROM tb_user WHERE id =#{id}")
    @Results({
 
    })
    User findUserAndUserInfo(int id);

    @Select("select * from user_info where id = #{id}")
    UserInfo findUserInfoById(int id);

    @Select("select * from tb_user where id =#{id}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "username",property = "username"),
            @Result(column = "birthday",property = "birthday"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "address",property = "address"),
            @Result(property = "orders",column = "id",many = @Many(select = "findOrdersById"))
    })
    User findUserAndOrdersById(int id);

    @Select("select * from `order` where user_id=#{id}")
    @Results({
            @Result(column = "o_id",property = "oId",id = true),
            @Result(column = "user_id",property = "userId"),
            @Result(column = "number",property = "number"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "note",property = "note")
    })
    List<Order> findOrdersById(int id);
}
